<%--
  Created by IntelliJ IDEA.
  User: 494418174
  Date: 2020/3/4 0004
  Time: 18:27
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta http-equiv="Access-Control-Allow-Origin" content="*">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="format-detection" content="telephone=no">
    <link rel="icon" href="/favicon.ico">
    <link rel="stylesheet" href="/layui/css/layui.css">
    <link rel="stylesheet" href="/layui_ext/dtree/dtree.css">
    <link rel="stylesheet" href="/layui_ext/dtree/font/dtreefont.css">
</head>
<body class="childrenBody">
<!-- 搜索条件开始 -->
<fieldset class="layui-elem-field layui-field-title" style="margin-top: 20px;">
    <legend>查询条件</legend>
</fieldset>
<form class="layui-form" method="post" id="searchFrm">

    <div class="layui-form-item">
        <div class="layui-inline">
            <label class="layui-form-label">车牌号:</label>
            <div class="layui-input-inline" style="padding: 5px">
                <input type="text" name="carNumber" autocomplete="off" class="layui-input layui-input-inline"
                       placeholder="请输入车牌号" style="height: 30px;border-radius: 10px">
            </div>
        </div>
        <div class="layui-inline">
            <label class="layui-form-label">车辆类型:</label>
            <div class="layui-input-inline" style="padding: 5px">
                <input type="text" name="carType" autocomplete="off" class="layui-input layui-input-inline"
                       placeholder="请输入车辆类型" style="height: 30px;border-radius: 10px">
            </div>
        </div>
        <div class="layui-inline">
            <label class="layui-form-label">车辆颜色:</label>
            <div class="layui-input-inline" style="padding: 5px">
                <input type="text" name="color" autocomplete="off" class="layui-input layui-input-inline"
                       placeholder="请输入车辆颜色" style="height: 30px;border-radius: 10px">
            </div>
        </div>
    </div>
    <div class="layui-form-item">
        <div class="layui-inline">
            <label class="layui-form-label">车辆描述:</label>
            <div class="layui-input-inline" style="padding: 5px">
                <input type="text" name="description" autocomplete="off" class="layui-input layui-input-inline"
                       placeholder="请输入车辆描述" style="height: 30px;border-radius: 10px">
            </div>
        </div>
        <div class="layui-inline">
            <label class="layui-form-label">是否出租:</label>
            <div class="layui-input-inline">
                <input type="radio" name="isrenting" value="1" title="已出租">
                <input type="radio" name="isrenting" value="0" title="未出租">
            </div>
            <button type="button"
                    class="layui-btn layui-btn-normal layui-icon layui-icon-search layui-btn-radius layui-btn-sm"
                    lay-submit lay-filter="formDemo" style="margin-top: 4px">查询
            </button>
            <button type="reset"
                    class="layui-btn layui-btn-warm layui-icon layui-icon-refresh layui-btn-radius layui-btn-sm"
                    style="margin-top: 4px">重置
            </button>
        </div>
    </div>

</form>

<!-- 搜索条件结束 -->

<table id="carTable" lay-filter="carTable"></table>

<script id="toolbarCar" type="text/html">
    <div class="layui-btn-container">
        <button class="layui-btn layui-btn-sm layui-btn-danger" lay-event="carAdd">添加</button>
        <button class="layui-btn layui-btn-sm layui-btn-danger" lay-event="carDelChk">批量删除</button>
    </div>
</script>

<script id="barCar" type="text/html">
    <a class="layui-btn layui-btn-xs layui-btn-radius" lay-event="edit">编辑</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs " lay-event="del">删除</a>
</script>


<!-- 添加和修改的弹出层-->
<div style="display: none;padding: 20px" id="saveOrUpdateDiv">
    <form class="layui-form layui-row layui-col-space10" lay-filter="dataFrm" id="dataFrm">
        <div class="layui-col-md12 layui-col-xs12">
            <div class="layui-row layui-col-space10">
                <div class="layui-col-md9 layui-col-xs7">

                    <div class="layui-form-item magt3">
                        <label class="layui-form-label">车牌号:</label>
                        <div class="layui-input-block" style="padding: 5px">
                            <input type="text" name="carNumber" id="carNumber" autocomplete="off" class="layui-input"
                                   lay-verify="required"
                                   placeholder="请输入车牌号" style="height: 30px;border-radius: 10px">
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">车辆类型:</label>
                        <div class="layui-input-block" style="padding: 5px">
                            <input type="text" name="carType" autocomplete="off" class="layui-input"
                                   placeholder="请输入车辆类型" style="height: 30px;border-radius: 10px">
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">车辆颜色:</label>
                        <div class="layui-input-block" style="padding: 5px">
                            <input type="text" name="color" autocomplete="off" class="layui-input"
                                   placeholder="请输入车辆颜色" style="height: 30px;border-radius: 10px">
                        </div>
                    </div>
                </div>
                <div class="layui-col-md3 layui-col-xs5">
                    <div class="layui-upload">
                        <button type="button" class="layui-btn" id="test1">上传图片</button>
                        <div class="layui-upload-list">
                         <img class="layui-upload-img" id="demo1" src="" style="width: 170px;height: 100px">
                            <p id="demoText"></p>
                            <input type="hidden" name="carimg" id="carimg">
                        </div>
                    </div>
                </div>
            </div>
            <div class="layui-form-item magb0">
                <label class="layui-form-label">车辆描述:</label>
                <div class="layui-input-block" style="padding: 5px">
                    <input type="text" name="description" autocomplete="off" class="layui-input"
                           placeholder="请输入车辆描述" style="height: 30px;border-radius: 10px">
                </div>
            </div>
            <div class="layui-form-item magb0">
                <div class="layui-inline">
                    <label class="layui-form-label">车辆价格:</label>
                    <div class="layui-input-block" style="padding: 5px">
                        <input type="text" name="price" class="layui-input" lay-verify="required|number"
                               placeholder="请输入车辆价格" style="height: 30px;border-radius: 10px">
                    </div>
                </div>
                <div class="layui-inline">
                    <label class="layui-form-label">出租价格:</label>
                    <div class="layui-input-block" style="padding: 5px">
                        <input type="text" name="rentPrice" class="layui-input" lay-verify="required|number"
                               placeholder="请输入车辆出租价格" style="height: 30px;border-radius: 10px">
                    </div>
                </div>
            </div>
            <div class="layui-form-item magb0">
                <div class="layui-inline">
                    <label class="layui-form-label">出租押金:</label>
                    <div class="layui-input-block" style="padding: 5px">
                        <input type="text" name="deposit" class="layui-input" lay-verify="required|number"
                               placeholder="请输入车辆出租押金" style="height: 30px;border-radius: 10px">
                    </div>
                </div>
                <div class="layui-inline">
                    <label class="layui-form-label">是否出租:</label>
                    <div class="layui-input-inline">
                        <input type="radio" name="isrenting" value="1" title="已出租">
                        <input type="radio" name="isrenting" value="0" checked="checked" title="未出租">
                    </div>
                </div>
            </div>
            <div class="layui-form-item magb0">
                <div class="layui-input-block" style="text-align: center;padding-right: 120px">
                    <button type="button"
                            class="layui-btn layui-btn-normal layui-btn-md layui-icon layui-icon-release layui-btn-radius"
                            lay-filter="doSubmit" lay-submit="">提交
                    </button>
                    <button type="reset"
                            class="layui-btn layui-btn-warm layui-btn-md layui-icon layui-icon-refresh layui-btn-radius">
                        重置
                    </button>
                </div>
            </div>
        </div>
    </form>
</div>
<%-- 添加或者修改弹出框结束 --%>

<script src="/layui/layui.js"></script>
<script>
    var carTable
    var index
    var url
    var uploadImg
    layui.extend({
        dtree: '/layui_ext/dtree/dtree'   // {/}的意思即代表采用自有路径，即不跟随 base 路径
    }).use(['table','form','layer','jquery','dtree','upload'], function() {
        var table = layui.table
        var form = layui.form
        var layer = layui.layer
        var $ = layui.jquery
        var dtree=layui.dtree
        var upload=layui.upload
        // 初始table
        carTable = table.render({
            elem: "#carTable"
            , url: "/car/findCar"
            , page: {limit: 5, limits: [5, 10, 15]}
            , cellMinWidth: 80
            , toolbar: '#toolbarCar'
            , cols: [[ //表头
                {type: 'checkbox', fixed: 'left'}
                , {field: 'carNumber', title: '车牌号', fixed: 'center'}
                , {field: 'carType', title: '车辆类型', fixed: 'center'}
                , {field: 'color', title: '车辆颜色', fixed: 'center'}
                , {field: 'price', title: '车辆价格', fixed: 'center'}
                , {field: 'rentPrice', title: '出租价格', fixed: 'center'}
                , {field: 'deposit', title: '出租押金', fixed: 'center'}
                , {field: 'isrenting', title: '是否出租', fixed: 'center' ,templet: function (data) {
                        return data.isrenting == 1 ? "是" : "否"
                    }}
                , {field: 'description', title: '车辆描述', fixed: 'center'}
                , {field: 'carimg', title: '图片', fixed: 'center',hide:true}
                , {field: 'createTime', title: '入库时间', fixed: 'center'}
                , {fixed: 'right', title: '操作', toolbar: '#barCar', width: 250}
            ]]
        })

        //图片上传
        uploadImg=upload.render({
            elem:"#test1"
            ,url:"/car/uploadImg"
            ,acceptMime:"image/*"
            ,done:function (res) {
                if(res.code>0){
                    return layer.msg("上传失败")
                }
                $("#demo1").attr("src",res.data.src)
                $("#carimg").val(res.data.src)
            }
        })
        // 查询
        form.on("submit(formDemo)", function (data) {
            carTable.reload({
                url: "/car/findCar"
                , where: {
                    'carNumber': data.field.carNumber
                    , 'carType':data.field.carType
                    ,'color':data.field.color
                    ,'description':data.field.description
                    ,'isrenting':data.field.isrenting

                }
                , page: {
                    curr: 1
                }
            })
            return false;
        })
        // 头部工具栏添加事件
        table.on('toolbar(carTable)',function(obj) {
            if(obj.event == "carAdd"){ // 点击新增按钮执行的内容
                carAdd()
            }else if(obj.event=="carDelChk"){
                carDelChk(obj)
            }
        })
        //监听行工具事件
        table.on('tool(carTable)', function(obj){
            var data = obj.data
            if(obj.event == "edit"){
                menuUpdate(data)
            }else  if(obj.event == "del"){
                del(data)
            }
        })


        function carDelChk(obj) {
            var checkStatus=table.checkStatus(obj.config.id);
            var data=checkStatus.data
            if(data.length==0){
                layer.msg("请至少选择一个数据")
            }else {
                var carIds=new Array()
                $.each(data,function (index,item) {
                    carIds.push(item.carNumber)
                })
                layer.confirm("你确定要删除"+data.length+"条数据么",function (ind) {
                    $.post("/car/carDelChk",{"ids":carIds},function (r) {
                        layer.msg(r.msg)
                        carTable.reload()
                    })
                    layer.close(ind)
                })
            }
        }
        function del(data){


            layer.confirm("您确定要删除"+ data.carNumber+"数据么",function(ind){

                $.post("/car/delCar", {"carNumber":data.carNumber} ,function(d) {
                    layer.msg(d.msg)
                    // 刷新table数据
                    carTable.reload()

                })
                layer.close(ind)
            })


        }
        function menuUpdate(data) {
            index =  layer.open({
                type:1
                , title:"修改车辆"
                , content:$("#saveOrUpdateDiv")
                , area : ['800px', '440px']
                , success:function(layero, index) {
                    form.val("dataFrm",data)
                    $("#demo1").attr("src",data.carimg)
                    $("#carNumber").addClass("layui-btn-disabled")
                    url="/car/updateCar"
                }
            })
        }
        function carAdd() {
            // 弹出框
            index =  layer.open({
                type:1 //
                , title:"添加车辆"
                , content:$("#saveOrUpdateDiv")
                , area : ['800px', '440px']
                , success:function(layero, index) {
                    // 页面数据要清除
                    $("#dataFrm")[0].reset()
                    $("#demo1").attr("src","/images/moren.jpg")
                    $("#carNumber").removeClass("layui-btn-disabled")
                    url="/car/addCar"
                }
            })

        }
        // form表达提交
        form.on("submit(doSubmit)",function() {
            layer.confirm("您确定要提交该数据么",function(ind){
                $.post(url,$("#dataFrm").serialize(),function(data) {
                    layer.msg(data.msg)
                    layer.close(index)
                    carTable.reload()
                })
                layer.close(ind)
            })





        })
    })

</script>
</body>
</html>
